﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil.CustomObject;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Text
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityTX0411", Name = "空格检证:两侧不是数字和英文时抽出", CategoryType = EnumCategoryType.文字列, Model = true)]
    public class VerfityTx0411 : VerfityOneResult
    {
        private CustomCollection<SpecialCell> _datalist = new CustomCollection<SpecialCell>() { Description = "指定数据列表" };


        [Category("忽略的检验的单元"), Description("忽略的检验的单元")]
        [TypeConverter(typeof(CustomCollectionConverter<SpecialCell>))]
        [RefreshProperties(RefreshProperties.All)]
        public CustomCollection<SpecialCell> DataList
        {
            get { return _datalist; }
            //set { _rangelist = value; }
        }

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;

            if (entryCommon == "30"  ||
                 entryCommon == "20")
            {
                return false;
            }

            for (int i = 0; i < result.Count; i++)
            {
                var rowResult = result[i];

                for (int j = 0; j < rowResult.Count; j++)
                {
                    var colResult = rowResult[j];

                    if (_datalist.Any())
                    {
                        //是否是忽略检查项目
                        if (DataList.Count(t =>
                                               {
                                                   if (t.Row.Equals(-1) && t.Col.Equals(-1))
                                                       return false;
                                                   if (t.Row.Equals( -1) && t.Col.Equals( j))
                                                        return true;
                                                   if (t.Row.Equals(i) && t.Col.Equals(-1))
                                                       return true;
                                                   if (t.Row.Equals(i) && t.Col.Equals(j))
                                                       return true;
                                                   return false;
                                               }) > 0)
                            continue;
                    }

                    bresult = CheckErrorResult(colResult);

                    if (bresult)
                        break;
                }
                if (bresult)
                    break;
            }

            //foreach (var rowResult in result)
            //{
            //    foreach (var colResult in rowResult)
            //    { 
                   
            //        bresult = CheckErrorResult(colResult);

            //        if (bresult)
            //            break;
 

            //    }

            //    if (bresult)
            //        break;
            //}


            if (bresult)
            {
                System.Console.WriteLine("VerfityTX0411"); 
            }
            return bresult;
        }

        /// <summary>
        /// 空格两侧不是数字和英文时抽出
        /// </summary>
        /// <param name="colResult"></param>
        /// <returns></returns>
        private bool CheckErrorResult(string colResult)
        {

            if (colResult.Contains(" "))
            {
               // var resultArray = colResult.ToCharArray();

                int starIndex = 0;
                int spaceIndex = -1;

                do
                {
                   
                   spaceIndex = colResult.IndexOf(" ", starIndex, System.StringComparison.CurrentCulture);

                   if (spaceIndex.Equals(0) || spaceIndex.Equals(colResult.Length - 1))
                        continue;

                   if (spaceIndex.Equals(-1))
                       break;

                   if (!char.IsLetterOrDigit(colResult, spaceIndex + 1) ||
                        !char.IsLetterOrDigit(colResult, spaceIndex - 1))
                   {
                       return true;
                   }
 
                    starIndex = spaceIndex + 1;

                } while (spaceIndex < 0 || starIndex > colResult.Length);


                
            }
            
          　
            return false;
        }


    }
}